Prettier: The Key to Beautifully Consistent Code π β
Ever spent more time formatting code than actually writing it? Say hello to Prettier, the opinionated code formatter that handles code styling for you! Prettier automates formatting, making sure your code is consistent, readable, and, well, pretty.
π‘ What is Prettier? β
Prettier is a code formatter that enforces a consistent style across your codebase. It handles indentation, line breaks, spacing, and even punctuation in supported languages like JavaScript, TypeScript, HTML, CSS, Markdown, and more. Prettierβs rules are opinionated, meaning it follows a strict style guideline to eliminate debates over code style.
π¬ Setting Up Prettier β
- Install Prettier: Add Prettier as a dev dependency in your project:
npm install prettier --save-devpnpm add -D prettieryarn add -D prettierbun add prettier --dev- Create a Prettier Config: Prettier works with zero configuration, but you can create a
.prettierrcfile to customize its behavior. Some common options include:
{
"printWidth": 80,
"tabWidth": 2,
"semi": false,
"singleQuote": true
}- Add Scripts: Add a script to
package.jsonto run Prettier across your project:
"scripts": {
"format": "prettier --write ."
}- Run Prettier: To format your code, simply run:
npm run formatpnpm run formatyarn formatbun formatπ Prettier Options You Should Know β
While Prettier is opinionated, you still have some flexibility. Here are a few key options you can set in .prettierrc:
printWidth: Max line length. Default is 80 characters.tabWidth: Number of spaces per indentation level.semi:truefor semicolons,falseto omit them.singleQuote: Use single quotes (true) or double quotes (false).trailingComma: Controls trailing commas ("none","es5", or"all").arrowParens: Controls parentheses around a single arrow function parameter ("avoid"or"always").
These options allow you to set preferences while still benefiting from Prettierβs strict and reliable styling.
π Integrating Prettier with ESLint β
Wanna know how it is done? Follow steps mentioned here.
π Automate with Prettier Hooks β
Prettier formats code, but ESLint ensures code quality. Together, theyβre unstoppable! Hereβs how to make them work side by side:
- Install Husky and lint-staged:
npm install husky lint-staged --save-devpnpm add -D husky lint-stagedyarn add -D husky lint-stagedbun add husky lint-staged --dev- Update
package.json: Inpackage.json, set up a lint-staged configuration:
"lint-staged": {
"*.js": ["prettier --write", "git add"]
}- Activate Husky: Set up Husky to run on the pre-commit hook:
npmx husky installpnpm dlx husky installyarn dlx husky installbun x husky installNow, every time you make a commit, Prettier will format staged files automatically, ensuring that all code stays consistent.
π Prettier in Your Editor β
Prettier plugins are available for most editors, so you can see the magic happen in real-time as you write code:
VS Code β
Install the Prettier - Code formatter extension. Set Prettier as your default formatter in VS Code by adding the following to your settings:
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": trueVim/Neovim: β
Integrate Prettier with Vim or Neovim using plugins like ALE or null-ls:
- With ALE, add this to
.vimrc:
let g:ale_fixers = {'javascript': ['prettier'], 'typescript': ['prettier']}
let g:ale_fix_on_save = 1'- With null-ls in Neovim:
require("null-ls").setup({
sources = {require("null-ls").builtins.formatting.prettier},
})π₯ Prettier Pro Tips for Maximum Efficiency β
- Use Prettier with CI/CD: Add Prettier as a step in your CI/CD pipeline to ensure consistent styling in all pull requests and prevent unformatted code from merging.
- Handle Markdown and JSON: Prettier can format Markdown and JSON files, making it a great tool for keeping documentation clean and readable.
- Ignore Files: Use a
.prettierignorefile to prevent certain files from being formatted, such as large JSON datasets or auto-generated files. - Combine with TypeScript: Prettier works well with TypeScript out of the box, making it a valuable tool for TypeScript projects by ensuring stylistic consistency.
π Wrapping Up β
Prettier is an essential tool for modern JavaScript and TypeScript projects. By handling code formatting automatically, it allows you to focus more on writing functional, clean code while ensuring it always looks great. Give it a go, and let Prettier bring the sparkle to your codebase!
